unit uDmUniSQLConexao;

interface

uses
  uMensagensSistema, uConexao, uConfiguracao, uMensagemConstante, uConstantesSistema,
  SysUtils, Classes, UniProvider, PostgreSQLUniProvider, DB, DBAccess, Uni, Forms;

type
  TdmUniSQLConexao = class(TDataModule)
    UniConnection: TUniConnection;
    PostgreSQLUniProvider: TPostgreSQLUniProvider;
    procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  dmUniSQLConexao: TdmUniSQLConexao;

  function inicializa(): integer;

implementation

function inicializa(): integer;
begin
   try
     result := retOK;
     if not assigned(dmUniSQLConexao) then
       Application.CreateForm(TdmUniSQLConexao,dmUniSQLConexao)
   except on e: exception do
    begin
       result := retErro;
       MensagemErro(MsgSemRede);
       Application.Terminate;
    end;
   end;
end;

{$R *.dfm}

procedure TdmUniSQLConexao.DataModuleCreate(Sender: TObject);
var
  conexao : TConexao;
begin
   try
     if UniConnection.Connected then
       UniConnection.Disconnect;

     conexao := Configuracao.BancoDeDados.retornarConexao();

     if assigned(conexao) then
      begin
         UniConnection.Username := conexao.usuario;
         UniConnection.Password := conexao.senha;
         UniConnection.Port     := conexao.porta;
         UniConnection.Server   := conexao.servidor;
         UniConnection.Database := conexao.banco;
         UniConnection.Connect;
      end;
   except on e: exception do
    begin
       UniConnection.Disconnect;
       MensagemErro(MsgErroConexao);
       Application.Terminate;
    end;
   end;
end;

end.
